﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Configuration.Provider;
using PizzaWic;

namespace PizzaWic.user
{
    public partial class GenericUserDesign : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void loginButton_Click(object sender, EventArgs e)
        {
            SqlAccess essai = new SqlAccess();
            String login = LoginTxtBox.Text;
            String password = PasswordTxtBox.Text;
            int id = essai.getUserId(login);
            if (essai.isRegistered(login, password))
            {
                if (essai.isAdministrator(id))
                {
                    String[] users = new String[1];
                    users[0] = login;

                    try
                    {
                        Roles.AddUsersToRole(users, "admin");
                        Roles.AddUsersToRole(users, "user");
                    }
                    catch (ProviderException ex)
                    {

                    }
                    FormsAuthentication.SetAuthCookie(login, false);
                    Session.Add("login", login);
                    Response.Redirect("..\\admin\\ProductManagement.aspx");
                }
                else
                    if (essai.isCooker(id))
                    {
                        int idCooker = essai.getCookerId(id);
                        String[] users = new String[1];
                        users[0] = login;
                        try
                        {
                            Roles.AddUsersToRole(users, "cooker");
                            Roles.AddUsersToRole(users, "user");
                        }
                        catch (ProviderException ex)
                        {

                        }
                        FormsAuthentication.SetAuthCookie(login, false);
                        Session.Add("login", login);
                        Session.Add("idCooker", idCooker);
                        Response.Redirect("..\\cooker\\DisplayCommand.aspx");
                    }
                    else
                    {
                        int idCustomer = essai.getCustomerId(id);
                        String[] users = new String[1];
                        users[0] = login;
                        try
                        {
                            Roles.AddUsersToRole(users, "customer");
                            Roles.AddUsersToRole(users, "user");
                        }
                        catch (ProviderException ex)
                        {

                        }
                        FormsAuthentication.SetAuthCookie(login, false);
                        Session.Add("login", login);
                        Session.Add("idCustomer", idCustomer);
                        //rajouter id customer et cooker
                        Response.Redirect("..\\customer\\DisplayProduct.aspx");
                    }

            }
            else
            {
                Response.Redirect("CreateAccount.aspx");
            }
        }
    }
}